<template>
  <div class="v-checklist" @change="$emit('change', currentValue)">
    <div class="v-checklist-title" v-if="title">{{title}}</div>
    <label
      class="v-checklist-item"
      v-for="option in options"
      :key="option.value || option"
    >
      <span class="v-checkbox">
        <input
          class="v-checkbox-input"
          type="checkbox"
          v-model="currentValue"
          :disabled="option.disabled"
          :value="option.value || option"
        >
        <span class="v-checkbox-check"></span>
      </span>
      <span class="v-checkbox-label" v-text="option.label || option"></span>
    </label>
  </div>
</template>
<script>
export default {
  name: 'Checklist',
  computed: {
    limit() {
      return this.max < this.currentValue.length
    },
  },
  props: {
    title: String,
    value: Array,
    max: Number,
    options: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      currentValue: this.value || [],
    }
  },
  watch: {
    value(val) {
      this.currentValue = val
    },

    currentValue(val) {
      if (this.limit) val.pop()
      this.$emit('input', val)
    },
  },
}
</script>
<style lang="scss">
@import "~styles/include";
.v-checklist-title {
  text-align: center;
  font-size: 16px;
  line-height: 48px;
  border-bottom: 1px solid $greyLineColor;
}

.v-checklist-item {
  display: block;
  padding: 12px;
  font-size: 14px;
}

.v-checkbox {
  float: right;
}

.v-checkbox-input {
  display: none;
  &:checked {
    + .v-checkbox-check {
      background-color: $primaryColor;
      border-color: $primaryColor;
      &::after {
        border-color: #fff;
        transform: rotate(45deg) scale(1);
      }
    }
  }
  &[disabled] + .mint-checkbox-core {
    background-color: #ccc;
    border-color: #ccc;
  }
}

.v-checkbox-check {
  display: inline-block;
  background-color: #fff;
  border: 2px solid #6B6B6B;
  border-radius: 2px;
  position: relative;
  width: 18px;
  height: 18px;
  vertical-align: middle;
  &::after {
    border: 2px solid transparent;
    border-left: 0;
    border-top: 0;
    content: " ";
    position: absolute;
    top: -1px;
    left: 4px;
    width: 7px;
    height: 13px;
    transform: rotate(45deg) scale(0);
    transition: transform .2s;
  }
}
</style>

